package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @RequestMapping("/findAll")
    public ModelAndView findAll(String page,String pageSize){
        ModelAndView modelAndView = new ModelAndView();
//        System.out.println(page+" "+pageSize);
        List<User> all =userService.findAll(page,pageSize);
        System.out.println(all);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("list",all);
        modelAndView.addObject("pageInfo",map);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.edit(id);
        modelAndView.addObject("user",user);
        modelAndView.setViewName("user-edit");
        return modelAndView;
    }
    @RequestMapping("/edit")
    public ModelAndView edit(User user){
        System.out.println(user);
        ModelAndView modelAndView = new ModelAndView();
        if(user.getId()==null){
            userService.save(user);
        }else{

            userService.editTo(user);

        }
        return this.findAll(null,null);
//           return null;

//            return this.findAll(null,null);



//        modelAndView.addObject("user",user);
//        modelAndView.setViewName("user-edit");



    }
}
